package com.trex.lwlsh.util;


import com.jayway.jsonpath.JsonPath;

import java.io.IOException;

/**
 * Create by Trex on 2018/1/19
 */
public class AssertUtil {



    public Boolean opinion(String ArrStrA, String jsonStr){
        Boolean ispassed = true;
        //"><="用来判断两个字符比较,":"
        try {
            if (ArrStrA.contains(">")||ArrStrA.contains("<")||ArrStrA.contains("=")||ArrStrA.contains(":")) {

                if (ArrStrA.contains(">")) {
                    String Arr = ArrStrA.substring(0, ArrStrA.indexOf(">"));
                    String Arr1 = ArrStrA.substring(ArrStrA.indexOf(">") + 1, ArrStrA.length());

                    double sortOne = Double.parseDouble(StrGetLS(Arr, jsonStr).trim());
                    double sortTne = Double.parseDouble(StrGetLS(Arr1, jsonStr).trim());
                    System.out.println("sortOne:>"+sortOne+"  sortTne:"+sortTne);

                    if (sortOne > sortTne) {
                        ispassed = true;
                    } else {
                        ispassed = false;
                    }
                }

                if (ArrStrA.contains("<")) {
                    String Arr = ArrStrA.substring(0, ArrStrA.indexOf("<"));
                    String Arr1 = ArrStrA.substring(ArrStrA.indexOf("<") + 1, ArrStrA.length());
                    System.out.println("Arr:"+Arr+"  Arr1:"+Arr1);
                    double sortOne = Double.parseDouble(StrGetLS(Arr, jsonStr).trim());
                    double sortTne = Double.parseDouble(StrGetLS(Arr1, jsonStr).trim());
                    System.out.println("sortOne:<"+sortOne+"  sortTne:"+sortTne);
                    if (sortOne < sortTne) {
                        ispassed = true;
                    } else {
                        ispassed = false;
                    }
                }
                if (ArrStrA.contains("=") && ArrStrA.contains("==") == false) {
                    String Arr = ArrStrA.substring(0, ArrStrA.indexOf("="));
                    String Arr1 = ArrStrA.substring(ArrStrA.indexOf("=") + 1, ArrStrA.length());

                    double sortOne  = Double.parseDouble(StrGetLS(Arr, jsonStr).trim());
                    double sortTne = Double.parseDouble(StrGetLS(Arr1, jsonStr).trim());
                    if (sortOne == sortTne) {
                        ispassed = true;
                    } else {
                        ispassed = false;
                    }
                }

                if (ArrStrA.contains(":")) {
                    if (getSubString(ArrStrA, ":") < 2 && ArrStrA.contains("\"") == false) {
                        String Arr = ArrStrA.substring(0, ArrStrA.indexOf(":"));
                        String Arr1 = ArrStrA.substring(ArrStrA.indexOf(":") + 1, ArrStrA.length());
                        String sortOne = StrGetLS(Arr, jsonStr).trim();
                        String sortTne = StrGetLS(Arr1, jsonStr);
                        if (sortOne.equals(sortTne)) {
                            ispassed = true;
                        } else {
                            ispassed = false;
                        }
                    }
                }}else{
                if(jsonStr.contains(ArrStrA.trim())){
                    ispassed = true;
                } else {
                    ispassed = false;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            ispassed = false;

        }

        return ispassed;
    }



    /**
     * 计算字符串中某字符出现次数
     * @param str
     * @param key
     * @return
     */
    public static int  getSubString(String str,String key) {
        int count = 0;
        int index = 0;
        while ((index = str.indexOf(key, index)) != -1) {
            index = index + key.length();
            count++;
        }
        return count;
    }

    /**
     * 把特殊字符全都转换
     * @param ArrStrA
     * @param JsonStr
     * @return
     * @throws Exception
     */
    public String StrGetLS(String ArrStrA, String JsonStr) throws Exception {
        String boolString = ArrStrA;
        if (ArrStrA.contains("+")) {
            try {
                String key1 = getvar(ArrStrA.substring(ArrStrA.lastIndexOf("+") + 1, ArrStrA.length()),JsonStr);
                String key2 = getvar(ArrStrA.substring(0, ArrStrA.lastIndexOf("+")),JsonStr);
                boolString=  ArrStrA=String.valueOf(Long.parseLong(key1)+Long.parseLong(key2));
            }catch(Exception e){
            }

        }
        if (ArrStrA.contains("-")) {
            try {
                String key1 = getvar(ArrStrA.substring(ArrStrA.lastIndexOf("-") + 1, ArrStrA.length()),JsonStr);
                String key2 = getvar(ArrStrA.substring(0, ArrStrA.lastIndexOf("-")),JsonStr);
                boolString=ArrStrA=String.valueOf(Long.parseLong(key2)-Long.parseLong(key1));
            }catch(Exception e){
                e.printStackTrace();
            }
        }

        if (ArrStrA.contains("%")) {
            try {
                String key1 = getvar(ArrStrA.substring(ArrStrA.lastIndexOf("%") + 1, ArrStrA.length()),JsonStr);
                String key2 = getvar(ArrStrA.substring(0, ArrStrA.lastIndexOf("%")),JsonStr);
                boolString=  ArrStrA=String.valueOf(Long.parseLong(key2)/Long.parseLong(key1));
            }catch(Exception e){
            }

        }
        if (ArrStrA.contains("*")) {
            try {
                String key1 = getvar(ArrStrA.substring(ArrStrA.lastIndexOf("*") + 1, ArrStrA.length()),JsonStr);
                String key2 = getvar(ArrStrA.substring(0, ArrStrA.lastIndexOf("*")),JsonStr);
                boolString=ArrStrA=String.valueOf(Long.parseLong(key2)*Long.parseLong(key1));
            }catch(Exception e){
            }
        }
        if (ArrStrA.contains("$")) {
            boolString=getvar( ArrStrA,  JsonStr);
        }

        return boolString;
    }


    /**
     *节点值提取
     * @param ArrStrA
     * @param JsonStr
     * @return
     * @throws Exception
     */
    public String getvar(String ArrStrA, String JsonStr) throws Exception {
        String boolString = ArrStrA;

        AssertUtil json =new  AssertUtil();
        while(ArrStrA.contains("$")) {
            if (ArrStrA.contains("$.")) {
                boolString=ArrStrA=json.JsonPath(JsonStr, ArrStrA);
                if(boolString.contains("\"")){
                    boolString=boolString.replace("\"","");
                }
            }

            if(ArrStrA.contains("$")==false){
                break;
            }
        }
        return boolString;
    }


    /**
     * json 表达式解析返回
     * @param json
     * @param expression
     * @return
     * @throws IOException
     */

    public   String  JsonPath(String json,String expression ) throws IOException {
        String GetValue=new String();

        try{
            if(json!=null&&expression!=null) {
                GetValue= JsonPath.read(json, expression).toString();
                if(GetValue.contains("[")){
                    GetValue=GetValue.substring(GetValue.indexOf("[")+1,GetValue.length()-1);
                }
            }
        }catch(Exception e){
            e.printStackTrace();
            System.out.println("JsonPath取值失败：");
        }

        return GetValue.replaceAll("\"","");
    }



    public static  void  main(String[]args) throws IOException {
        String jsonStr="{\"store\":{\"book\":[{\"category\":\"reference\",\"author\":\"Nigel Rees\",\"title\":\"Sayings of the Century\",\"price\":8.95},{\"category\":\"fiction\",\"author\":\"Evelyn Waugh\",\"title\":\"Sword of Honour\",\"price\":12.99},{\"category\":\"fiction1\",\"author\":\"Herman Melville\",\"title\":\"Moby Dick\",\"isbn\":\"0-553-21311-3\",\"price\":8.99},{\"category\":\"fiction23\",\"author\":\"32J. R. R. Tolkien\",\"title\":\"The Lord of the Rings\",\"isbn\":\"0-395-19395-8\",\"price\":22.99}],\"bicycle\":{\"color\":\"red\",\"price\":19.95}}}";
        AssertUtil json=new   AssertUtil();
        String expression1="$..price";//获取所有节点下的price
        String expression2="$..book[1].price";//获取所有书籍里的第二个价格
        String expression3="$.store.book[?(@.category == 'reference')]";//获取Book category 为reference 的书籍
        String expression4="$.store.book[?(@.isbn)]";//获取有 isbn 的书籍
        String expression5="$.store.book[?(@.price > 10)]";//获取Book 价格大于10 的书籍
        String expression6="$..book[-1:]";//获取最后一本书
        String expression10="$..book[-2:]";//获取最后两本书
        String expression7="$.store.book[0].title";//获取第一本书的书名
        String expression8="$.store.bicycle.color";//获取自动车的颜色
        String expression9="$.store.book[*].author";//获取所有的书本作者

        System.out.println("info1:"+json.JsonPath(jsonStr,expression1));
        System.out.println("info2:"+json.JsonPath(jsonStr,expression2));
        System.out.println("info3:"+json.JsonPath(jsonStr,expression3));
        System.out.println("info4:"+json.JsonPath(jsonStr,expression4));
        System.out.println("info5:"+json.JsonPath(jsonStr,expression5));
        System.out.println("info6:"+json.JsonPath(jsonStr,expression6));
        System.out.println("info7:"+json.JsonPath(jsonStr,expression7));
        System.out.println("info8:"+json.JsonPath(jsonStr,expression8));
        System.out.println("info9:"+json.JsonPath(jsonStr,expression9));
        System.out.println("info9:"+json.JsonPath(jsonStr,expression10));
        System.out.println("sfasd");
    }

}
